<!--
  移动签批->公文详情->收文->修改公文要素
-->
<template>
  <view>
    <van-popup v-model="showModel" closeable round position="bottom" :style="{ height: '100%' }"
               @click-close-icon="toClose" @click-overlay="toClose">
      <van-row type="flex" justify="center">
        <van-col offset="1" span="8">
          <van-cell value="修改公文要素"/>
        </van-col>
      </van-row>
      <van-form>
        <van-collapse v-model="tableShow" accordion :border="border">
          <van-collapse-item icon="label-o" title="公文要素" title-class="text-bold" name="jbxx">

            <van-cell is-link required title="紧急程度"
                      :value="this.dict.getDictNameByDictCode('swjjcd',formData.emergencyLevel)"
                      @click="showJjcd = true"/>
            <van-action-sheet v-model="showJjcd" :actions="jjcd" cancel-text="取消" @select="onSelectJjcd"/>

            <van-cell is-link title="行政许可类公文" :value="convertIsNot(formData.xingzhengxuke)" @click="showXzxk = true"/>
            <van-action-sheet v-model="showXzxk" :actions="isNotList" cancel-text="取消" @select="onSelectXzxk"/>

            <van-field v-model="formData.xingzhengshenpiNum" label="行政审批事项编号" input-align="right"/>

            <van-field required v-model="formData.title" label="文件标题" input-align="right" autosize rows="1" type="textarea" />

            <van-cell is-link required title="是否归档" :value="convertIsNot(formData.archive)" @click="showGd = true"/>
            <van-action-sheet v-model="showGd" :actions="isNotList" cancel-text="取消" @select="onSelectGd"/>

            <van-field required label="来文单位" v-model="formData.fwDeptName" input-align="right"/>

            <van-cell is-link title="来文编号" v-model="formData.fwCode" @click="showGenerate('lw')"/>
            <generate @closeGenerate="closeGenerate" @saveGenerate="saveGenerate" :tagType="tagType"
                      :showGenerate="showLw" :generateParamObject="generateParamObject"></generate>

            <van-cell is-link required title="收文编号" v-model="formData.swCode" @click="showGenerate('sw')"/>
            <generate @closeGenerate="closeGenerate" @saveGenerate="saveGenerate" :tagType="tagType"
                      :showGenerate="showSw" :generateParamObject="generateParamObject"></generate>

            <van-field label="收文时间" :value="formData.swTime" @click="showSwsj = true" input-align="right" is-link/>
            <van-calendar v-model="showSwsj" :show-confirm="false" @confirm="onConfirmSwsj"/>

            <!-- 收文来源 -->
            <van-cell is-link title="收文来源" :value="this.dict.getDictNameByDictCode('source',formData.source)"
                      @click="showSwly = true"/>
            <van-action-sheet v-model="showSwly" :actions="swlyList" cancel-text="取消" @select="onSelectSwly"/>

            <!-- 文种 -->
            <van-cell is-link title="公文种类" :value="this.dict.getDictNameByDictCode('swLanguage',formData.swLanguage)"
                      @click="showWz = true"/>
            <van-action-sheet v-model="showWz" :actions="wzList" cancel-text="取消" @select="onSelectWz"/>

            <!-- 收文内容 -->
            <van-cell is-link title="收文内容" :value="this.dict.getDictNameByDictCode('content',formData.content)"
                      @click="showSwnr = true"/>
            <van-action-sheet v-model="showSwnr" :actions="swnrList" cancel-text="取消" @select="onSelectSwnr"/>

            <van-field v-model="formData.remark" label="备注" rows="2" autosize type="textarea" input-align="right"/>

          </van-collapse-item>
        </van-collapse>
      </van-form>

      <view class="padding-top-lg padding-bottom-lg">
        <van-row type="flex" justify="space-around">
          <van-col span="10">
            <van-button type="info" round plain hairline block @click="toClose">取消</van-button>
          </van-col>
          <van-col span="10">
            <van-button type="info" round block @click="toEdit">确定</van-button>
          </van-col>
        </van-row>
      </view>
    </van-popup>
  </view>
</template>

<script>
import generate from 'pages/caac/components/receiveDoc/compo-generate';

export default {

  components: {
    generate
  },
  props: {
    showEdit: {
      type: Boolean,
      default: false
    },
    detailObj: { // 公文详情数据
      type: Object
    }
  },
  data() {
    return {
      showModel: false, // 显示组件
      formData: {},
      // 紧急程度
      showJjcd: false,
      jjcd: [{
        name: '平件'
      }, {
        name: '急件'
      }, {
        name: '特急'
      }
      ],
      // 来文/收文字号
      showLw: false,
      showSw: false,
      tagType: '',
      generateParamObject: {},
      // 收文时间
      showSwsj: false,
      // 当前展开面板的 name
      tableShow: 'jbxx',
      // 是否显示外边框,默认true
      border: false,
      isNotList: [      // 行政许可
        {name: '是', value: '1'},
        {name: '否', value: '0'}
      ],
      showXzxk: false,
      swlxList: [       // 收文类型
        {name: '外部来文', value: '1'},
        {name: '内部来文', value: '0'}
      ],
      showGd: false,
      showSwlx: false,
      swlyList: [],     // 收文来源
      showSwly: false,
      wzList: [],       // 文种
      showWz: false,
      swnrList: [],     // 收文内容
      showSwnr: false,
    };
  },
  mounted() {
    // 获取字典项
    this.getDict()
  },
  watch: {
    showEdit(val) {
      this.showModel = val;
    },
    detailObj(val) {
      this.formData = val;
      this.generateParamObject.swWord = val.swWord;
      this.generateParamObject.swWordId = val.swWordId;
      this.generateParamObject.swYear = val.swYear;
      this.generateParamObject.swNum = val.swNum;
      this.generateParamObject.fwWord = val.fwWord;
      this.generateParamObject.fwYear = val.fwYear;
      this.generateParamObject.fwNum = val.fwNum;
    }
  },
  methods: {
    // 紧急程度
    onSelectJjcd(item) {
      this.formData.emergencyLevel = item.dictItemCode
      this.showJjcd = false;
    },
    // 行政许可
    onSelectXzxk(item) {
      this.formData.xingzhengxuke = item.value
      this.showXzxk = false
    },
    convertIsNot(value) {
      if (value == '1') {
        return '是'
      }
      if (value == '0') {
        return '否'
      }
    },
    // 归档
    onSelectGd(item) {
      this.formData.archive = item.value
      this.showGd = false
    },
    // 收文类型
    onSelectSwlx(item) {
      this.formData.processType = item.value
      this.showSwlx = false
    },
    convertSwlx(value) {
      if (value == '1') {
        return '外部来文'
      }
      if (value == '0') {
        return '内部来文'
      }
    },
    // 收文来源
    onSelectSwly(item) {
      this.formData.source = item.dictItemCode
      this.showSwly = false;
    },
    // 文种
    onSelectWz(item) {
      this.formData.swLanguage = item.dictItemCode
      this.showWz = false;
    },
    // 收文内容
    onSelectSwnr(item) {
      this.formData.content = item.dictItemCode
      this.showSwnr = false;
    },

    // 显示字号组件
    showGenerate(type) {
      if (type == 'lw') {
        this.tagType = 'lw'
        this.showLw = true
      }
      if (type == 'sw') {
        this.tagType = 'sw'
        this.showSw = true
      }
    },
    // 关闭字号组件
    closeGenerate() {
      this.showLw = false
      this.showSw = false
    },
    // 保存字号
    saveGenerate(data) {
      if (data.type == 'lw') {
        this.formData.fwWord = data.lwWord
        this.formData.fwYear = data.lwYear
        this.formData.fwNum = data.lwNum
        this.formData.fwCode = this.generateCode(data.lwWord, data.lwYear, data.lwNum)
        this.showLw = false
      }
      if (data.type == 'sw') {
        this.formData.swWord = data.swWord
        this.formData.swYear = data.swYear
        this.formData.swNum = data.swNum
        this.formData.swWordId = data.swWordId
        this.formData.swCode = this.generateCode(data.swWord, data.swYear, data.swNum)
        this.showSw = false
      }
    },
    // 生成编号
    generateCode(word, year, num) {
      if (word != null && word != ''
          && year != null && year != ''
          && num != null && num != '') {
        return word + '〔' + year + '〕' + num + '号'
      }
    },

    // 收文时间
    onConfirmSwsj(date) {
      const formatDate = (date) => `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
      this.showSwsj = false
      this.formData.swTime = formatDate(date)
    },

    // 取消按钮
    toClose() {
      this.$emit('closeEdit', this.showModel);
    },

    // 确定按钮
    toEdit() {
      this.$dialog.confirm({
        confirmButtonColor: '#1989fa',
        title: '系统提示',
        message: '确认修改吗?',
      }).then(() => {
        if (this.validateForm()) {
          this.saveElement()
        }
      }).catch(() => {
        // oncancel
      })
    },

    // 校验表单
    validateForm() {
      // 紧急程度
      if (this.util.isEmpty(this.formData.emergencyLevel)) {
        this.$toast("请选择紧急程度")
        return false
      }
      // 标题
      if (this.util.isEmpty(this.formData.title)) {
        this.$toast("请填写标题")
        return false
      }
      // 归档
      if (this.util.isEmpty(this.formData.archive)) {
        this.$toast("请选择是否归档")
        return false
      }
      // 来文单位
      if (this.util.isEmpty(this.formData.fwDeptName)) {
        this.$toast("请填写来文单位")
        return false
      }
      // 收文编号
      if (this.util.isEmpty(this.formData.swcode)) {
        this.$toast("请填写收文编号")
        return false
      }
      return true
    },

    // 修改公文要素
    saveElement() {
      var _this = this
      this.$httploading({
        url: this.$http.adornUrl(`/caac/oa/authority/sw?systemKey=` + this.cache.get('userInfo').systemKey + `&systemFlag=` + this.cache.get('userInfo').systemFlag),
        method: 'post',
        data: JSON.stringify(this.formData)
      }).then(({data}) => {
        if (data.code == 200) {
          _this.$toast('修改公文要素成功')
          _this.$emit('closeEdit', this.showModel);
          _this.$emit('refreshBtnGroup', true) //修改成功后通知父组件重新加载数据
        }
      })
    },

    //父组件点击修改公文要素时调用获取详情回显到表单
    editGw(proInstId) {
      console.log("获取公文要素详情" + proInstId);
      var _this = this
      this.$httploading({
        url: this.$http.adornUrl(`/caac/oa/authority/gljfw/proInstId/` + proInstId),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag//固定值
        })
      }).then(({data}) => {
        if (data.code == 200) {
          _this.formData = data.data
          _this.getRelation(data.data.extendIdForRelate)
        }
      })
    },

    //获取需要的字典项
    getDict() {
      // 紧急程度
      const dictEmergencyLevel = this.cache.get('swjjcd')
      this.jjcd = []
      dictEmergencyLevel.forEach((item) => {
        this.jjcd.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })
      // 收文来源
      const dictSource = this.cache.get('source')
      this.swlyList = []
      dictSource.forEach((item) => {
        this.swlyList.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })
      // 文种
      const dictLanguage = this.cache.get('swLanguage')
      this.wzList = []
      dictLanguage.forEach((item) => {
        this.wzList.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })
      // 收文内容
      const dictContent = this.cache.get('content')
      this.swnrList = []
      dictContent.forEach((item) => {
        this.swnrList.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })
    },
  },

};
</script>

<style scoped>

	.van-cell__title {
		min-width: 55%;

		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}

	/deep/ .van-collapse-item__content {
		padding: 0px;
		color: #969799;
		font-size: 14px;
		line-height: 1.5;
		background-color: #fff;
	}

	/deep/ .van-field__label {
		color: #323233;
		min-width: 50%;

		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}

	/deep/ .van-field__control {
		font-size: 14px;
		color: #969799;
	}

	.radio_float {
		float: right;
	}
</style>
